package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.ProCharacterLibrary;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("show_pro_character_libraryService")
public class Show_Pro_Character_libraryService {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private SessionFactory hibernateSessionFactory;

    public String show_protocol_bank(String iDisplayStart, String iDisplayLength) {

        BaseDao baseDao = null;

        JSONObject json = new JSONObject();
        JSONArray result = new JSONArray();

        try {
            Session session = hibernateSessionFactory.openSession();
            session.clear();
            String sql = "select * from ProCharacterLibrary";
            Object[] params = {};
            ResultSet rs = baseDao.executeSQL(sql, params);
            int length = 0;
            List li = new ArrayList();
            while (rs.next()){
                length++;
                li.add(rs.next());
            }
//            String hql = "From ProCharacterLibrary";
//            Query query = session.createQuery(hql);
//            int length = query.list().size();
//            query.setFirstResult(Integer.parseInt(iDisplayStart));
//            query.setMaxResults(Integer.parseInt(iDisplayLength));
//            List li = query.list();
//            session.close();

            json.put("iTotalRecords", length);
            json.put("iTotalDisplayRecords", length);
            for (Object object : li) {
                ProCharacterLibrary procl = (ProCharacterLibrary) object;
                JSONArray one = new JSONArray();
                one.add(procl.getId());
                one.add(procl.getName());
                one.add(procl.getPath());
                one.add(sdf.format(procl.getUptime()));
                one.add("<a class='red' href='javascript:void(0)' title='删除' onclick='delectObj(" + procl.getId() + ")'><i class='icon-trash bigger-130'></i></a>");
                result.add(one);
            }
            json.put("aaData", result);
        } finally {
            return json.toString();
        }

    }
}
